<?php

defined('MYLITS') ? '' : exit('No direct script access allowed');

/**
 * @version     1.0.0
 * @package     mylits engine
 * @license     GNU General Public License version 3
 * @author      Arfan Fudyartanto D N <arfan@mylits.com> - http://engine.mylits.com
 */
class editProcess extends Mylits_Process {

    function __construct() {
        parent::__construct();
        $this->model = $this->loadModel('users');
        $this->loadHelper('users');
    }

    public function response() {
        $msgClass = '';
        $msg = array();
        if (!empty($_POST)) {
            extract($_POST);
            $userId = Mylits_Security::decrypt($core_user_id);
            $updatePassword = false;

            #execute base user input validation
            $val = usersHelper::userInputValidation($this->model);
            extract($val);

            #validation edit
            if (!empty($core_user_password)) {
                $updatePassword = true;
                if (($core_user_password != $core_user_cpassword)) {
                    $msgClass = 'warning';
                    $msg[] = Mylits_Language::getText('password not match');
                }
            }

            $user = Mylits_User::getInstance();
            if (($core_user_username != $user::$data->username) && $this->model->isUsernameExist($core_user_username)) {
                $msgClass = 'warning';
                $msg[] = Mylits_Language::getText('username_exist');
            }
            if (empty($msg)) {
                $this->model->startTrans();
                $result = $this->model->updateUser($core_user_username, $core_user_email, $core_user_name, $userId);
                if ($result) {
                    $result = $this->model->deleteGroupMap($userId);
                }
                if ($result) {
                    foreach ($group as $groupId) {
                        $result = $this->model->addGroupMap($userId, $groupId);
                        if (!$result) {
                            break;
                        }
                    }
                }
                if($updatePassword && $result){
                    $core_user_password = Mylits_Security::asymmetricEncrypt($core_user_password);
                    $result = $this->model->updatePassword($core_user_password, $userId);
                }
                $this->model->endTrans($result);

                if ($result) {
                    $msgClass = 'success';
                    $msg[] = Mylits_Language::getText('success');
                } else {
                    $msgClass = 'warning';
                    $msg[] = Mylits_Language::getText('failed');
                }
            }
        }
        Mylits_System::addSystemMsg($msgClass, implode(', ', $msg));
        Mylits_Messenger::sent($this->getComponentName(), $result);
        Mylits_System::redirect($this->getComponentName() . '/view.edit/' . $core_user_id);
    }

}

?>
